﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VsSourceCpoy.Helper;
using VsSourceCpoy.Service;
using System.IO;
using VsSourceCpoy.Entity;

namespace VsSourceCpoy
{
    public class Program
    {
        static void Main(string[] args)
        {
            //args = new string[] { @"F:\myproject\CodeGenerator", @"F:\toPathVS2008", "/a" };
            int argLength = 0;
            if (args == null || (argLength = args.Length) == 0 || args[0].Equals("/?"))
            {
                Usage();
                return;
            }
            else if (argLength >= 2)
            {
                foreach (var item in args)
                {
                    Console.WriteLine(item);
                }
                string fileName = GetFileName(args[0]);
                if (string.IsNullOrEmpty(fileName))
                {
                    Console.WriteLine("源文件/目录名无效 ：" + args[0]);
                    return;
                }
                SolutionCopyContext context = new SolutionCopyContext()
               {
                   OverWrite = false,
                   FromFileName = fileName,
                   ToPath = args[1],

               };
                if (argLength >= 3)
                {
                    var commandArray = args.Skip(2);
                    foreach (var item in commandArray)
                    {
                        if (item.Equals("/y", StringComparison.OrdinalIgnoreCase))
                        {
                            context.OverWrite = true;
                        }
                        else if (item.Equals("/a", StringComparison.OrdinalIgnoreCase))
                        {
                            context.CopyListener = CopyListener;
                        }
                        else
                        {
                            Console.WriteLine("未知命令，拷贝终止！");
                            return;
                        }
                    }
                }

                if (Directory.Exists(context.ToPath) && !context.OverWrite)
                {
                    Console.WriteLine("目录：" + context.ToPath + " 已经存在，是否删除原目录中文件，请输入y删除目录内容，输入n终止拷贝!");
                    string userInput = Console.ReadLine();
                    if ("y".Equals(userInput, StringComparison.OrdinalIgnoreCase))
                    {
                        context.OverWrite = true;
                    }
                    else
                    {
                        Console.WriteLine("拷贝终止！");
                        return;
                    }
                }

                try
                {
                    ISolutionCopy cssc = new CSharpSolutionCopy();
                    SolutionCopyResult scr = cssc.Copy(context);
                    Console.WriteLine();
                    Console.WriteLine("拷贝成功！");
                    ShowStatistical(scr);
                }
                catch (VersionUnsupportException e)
                {
                    Console.WriteLine("拷贝失败：");
                    Console.WriteLine(e.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("拷贝失败：");
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private static void ShowStatistical(SolutionCopyResult scr)
        {
            Console.WriteLine("拷贝项目版本：" + scr.VSVersion);
            string totalFile = "拷贝总文件数：" + scr.CopyedFileList.Count;
            Console.WriteLine(totalFile);
            string slnFile = "拷贝工程数：" + scr.CopyedSlnList.Count;
            Console.WriteLine(slnFile);
            string csprojFile = "拷贝项目数：" + scr.CopyedCsprojList.Count;
            Console.WriteLine(csprojFile);
            string ignFile = "忽略文件目数：" + scr.IgnoreList.Count;
            Console.WriteLine(ignFile);
            if (scr.IgnoreList.Count > 0)
            {
                Console.WriteLine("被忽略的文件：");
                foreach (var item in scr.IgnoreList)
                {
                    Console.WriteLine(item);
                }
            }
        }

        private static void CopyListener(string from, string to)
        {
            StringBuilder sb = new StringBuilder(200);
            sb.Append("正在拷贝，从:'");
            sb.Append(new FileInfo(from).FullName);
            sb.Append(", 到 '");
            sb.Append(new FileInfo(to).FullName);
            sb.Append("' 文件大小：");
            sb.Append(new FileInfo(from).Length / 1024 + "kb");
            Console.WriteLine(sb);
        }

        private static void Usage()
        {
            string usage = Environment.NewLine + "拷贝一个VS2008或VS2010工程。如果文件路径中有空格，请把文件名放在引号中。" + Environment.NewLine + Environment.NewLine + "VsSourceCpoy source destination [/Y][/A]" + Environment.NewLine + "/Y:如果目标目录已经存在，则不提示直接覆盖" + Environment.NewLine + "/A 显示详细拷贝信息。";
            Console.WriteLine(usage);
        }

        private static string GetFileName(string fileNameInput)
        {
            string fileNameRet = fileNameInput;
            if (!File.Exists(fileNameInput))
            {
                DirectoryInfo di = new DirectoryInfo(fileNameInput);
                if (di.Exists)
                {
                    var slnFile = di.GetFiles().Where(f => ".sln".Equals(f.Extension, StringComparison.OrdinalIgnoreCase));
                    if (slnFile.Count() > 0)
                    {
                        fileNameInput = slnFile.First().FullName;
                    }
                    else
                    {
                        var projFile = di.GetFiles().Where(f => ".csproj".Equals(f.Extension, StringComparison.OrdinalIgnoreCase));
                        if (projFile.Count() > 0)
                        {
                            fileNameInput = projFile.First().FullName;
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                else
                    return null;
            }
            return fileNameInput;
        }
    }
}
